package cate.game.role.bag.awakenfairy;

import cate.common.table.d.GDObj;
import cate.common.table.item.awakenfairy.row.AwakenFairyBaseRow;
import cate.common.table.item.awakenfairy.row.AwakenFairyPosRow;
import cate.common.util.GameResult;
import cate.game.attr.AwakenFairysPowerAttrPart;
import cate.game.attr.PowerAttrPart;
import cate.game.play.support.PlayBuildContext;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.bag.IBagContainer;
import cate.game.role.bag.hero.Hero;
import cate.game.role.bag.hero.OnHeroItem;
import com.fasterxml.jackson.annotation.JsonIgnore;
import cp.solution.util.collection.MapKit;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.springframework.data.annotation.Transient;

import java.util.HashMap;
import java.util.Map;

@NoteClass(value = "英雄身上的觉醒灵器")
public class AwakenFairiesOn extends RoleDependent implements OnHeroItem<AwakenFairy> {
    @JsonIgnore
    @Transient
    public transient Hero owner;

    @NoteField("觉醒灵器栏")
    public Map<Byte, AwakenFairy> awakenFairyMap;

    public AwakenFairiesOn() {

    }

    public void initialize(Hero owner, Role role){
        super.initialize(role);
        this.owner = owner;
        if(awakenFairyMap == null){
            awakenFairyMap = new HashMap<>();
        }
        awakenFairyMap.values().forEach(a -> a.initialize(role));
    }

    @Override
    @JsonIgnore
    public IBagContainer<AwakenFairy> getItemBag() {
        return role.getBag().getBag(GDObj.Type.AWAKEN_FAIRY);
    }

    @Override
    public Map<Byte, AwakenFairy> getPosItems() {
        return awakenFairyMap;
    }

    @Override
    public void doRealOn(byte pos, AwakenFairy awakenFairy) {
        // 穿上装备
        awakenFairyMap.put(pos, awakenFairy);
    }

    @Override
    public void doRealOff(byte pos, AwakenFairy awakenFairy) {
        this.awakenFairyMap.remove(pos);
    }

    @Override
    @JsonIgnore
    public PowerAttrPart getPowerAttrPart(PlayBuildContext ctx) {
        return new AwakenFairysPowerAttrPart(ctx, this);
    }

    @Override
    public AwakenFairiesOn copy() {
        AwakenFairiesOn cp = new AwakenFairiesOn();
        cp.awakenFairyMap = MapKit.cloneDeep(this.awakenFairyMap);
        cp.initialize(this.owner, this.role);
        return cp;
    }

    @Override
    public GameResult<Void> preCheckOn(PlayBuildContext ctx, byte pos, AwakenFairy awakenFairy){
        AwakenFairyBaseRow row = awakenFairy.getTpl(ctx);
        GameResult<Void> r = new GameResult<>();
        if (row == null) {
            return r.fail("觉醒灵器配置有误");
        }
        AwakenFairyPosRow posRow = ctx.awakenFairyExcel().pos.getRowByPos(pos);
        if (posRow == null) {
            return r.fail("穿戴栏位有误");
        }
        if (owner.getTpl(ctx).star < posRow.star) {
            return r.fail("英雄星级不够");
        }
        if (owner.getEffectiveLevel() < posRow.level) {
            return r.fail("英雄等级不够");
        }
        if (awakenFairyMap.entrySet().stream()
                .anyMatch(e -> e.getValue().getTpl(ctx).protoId == awakenFairy.getTpl(ctx).protoId && e.getKey() != pos)) {
            return r.fail("无法穿戴相同的觉醒灵器");
        }
        return r.success();
    }
}
